package vn.lumi.smarthome.ui;

import vn.lumi.smarthome.R;
import vn.lumi.smarthome.io.database.DatabaseHelper;
import vn.lumi.smarthome.ui.MainFragment.OnTabSelectedListener;
import vn.lumi.smarthome.ui.MainFragment.TABS;
import vn.lumi.smarthome.ui.MenuFragment.OnFloorRoomClickListener;
import vn.lumi.smarthome.ulti.LogUltis;
import android.content.Intent;
import android.os.Bundle;
import android.support.v4.app.FragmentManager;
import android.widget.Toast;

import com.actionbarsherlock.view.Menu;
import com.actionbarsherlock.view.MenuItem;
import com.slidingmenu.lib.SlidingMenu;
import com.slidingmenu.lib.app.SlidingFragmentActivity;

public class MainActivity extends SlidingFragmentActivity implements OnFloorRoomClickListener,OnTabSelectedListener{
	private static final String TAG="MainActivity";
	private MenuItem mMenuItem_AddScene;
	private MenuItem mMenuItem_ModifyScene;
	
	//Cac bien luu gia tri room_id vs floor_id dang duoc chon
	private int mRoomId;
	private int mFloorId;
	
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		DatabaseHelper.copyDataBase(getApplicationContext(), "smarthome.db");
		setBehindContentView(R.layout.activity_main_behind);
		setContentView(R.layout.activity_main);
		
		setupSlidingMenu();
		setupActionBar();
		setupMenu();
		setHomeScreen();
	}
	
	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		com.actionbarsherlock.view.MenuInflater inflater=getSupportMenuInflater();
		inflater.inflate(R.menu.menu_scene, menu);
		mMenuItem_AddScene=menu.findItem(R.id.menu_scene_add);
		mMenuItem_ModifyScene=menu.findItem(R.id.menu_scene_modify);
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem item) {
		switch (item.getItemId()) {
		case android.R.id.home:
			getSlidingMenu().showMenu(true);
			break;
		case R.id.menu_scene_add:
//			AddSceneFragment addSceneFragment=AddSceneFragment.newInstance(mFloorId, mRoomId);
//			addSceneFragment.show(getSupportFragmentManager(), "add_scene");
			Intent intent=new Intent(this,AddSceneActivity.class);
			intent.putExtra(AddSceneActivity.EXTRA_FLOOR_ID, mFloorId);
			intent.putExtra(AddSceneActivity.EXTRA_ROOM_ID, mRoomId);
			startActivity(intent);
			break;
		case R.id.menu_scene_modify:
			Toast.makeText(getApplicationContext(), "Dang tich hop", Toast.LENGTH_LONG).show();
//			intent=new Intent(this,DeleteSceneActivity.class);
//			startActivity(intent);
			break;
		default:
			break;
		}
		return super.onOptionsItemSelected(item);
	}
	
	private void setupSlidingMenu(){
		SlidingMenu slidingMenu=getSlidingMenu();
		slidingMenu.setBehindOffsetRes(R.dimen.sliding_behind_offset);
		slidingMenu.setShadowWidthRes(R.dimen.sliding_shadow_width);
		slidingMenu.setFadeDegree(0.35f);
		slidingMenu.setTouchModeAbove(SlidingMenu.TOUCHMODE_NONE);
	}

	private void setupActionBar(){
		getSupportActionBar().setHomeButtonEnabled(true);
		getSupportActionBar().setBackgroundDrawable(getResources().getDrawable(R.drawable.bg_actionbar));
	}
	private void setupMenu(){
		MenuFragment menuFragment=new MenuFragment();
		getSupportFragmentManager().beginTransaction().replace(R.id.activity_main_behind_content, menuFragment, "menu").commit();
	}
	private void setHomeScreen(){
		mRoomId=1;
		mFloorId=1;
		FragmentManager fragmentManager=getSupportFragmentManager();
		MainFragment fragment=(MainFragment) fragmentManager.findFragmentByTag("main");
		if(fragment==null){
			MainFragment mainFragment=MainFragment.newInstance(mFloorId,mRoomId);
			getSupportFragmentManager().beginTransaction().replace(R.id.activity_main_content, mainFragment, "main").commit();
		}else{
			fragment.refreshData(mFloorId, mRoomId);
		}
	}

	@Override
	public void onFloorRoomClick(int floor_id, int room_id,String floor_name,String room_name) {
		LogUltis.i(TAG, "Floor id=["+floor_id+" vs room_id=["+room_id);
		getSlidingMenu().showContent(true);
		if(floor_id==mFloorId&&room_id==mRoomId) return;
		mRoomId=room_id;
		mFloorId=floor_id;		
		FragmentManager fragmentManager=getSupportFragmentManager();
		MainFragment fragment=(MainFragment) fragmentManager.findFragmentByTag("main");
		if(fragment==null){
			MainFragment mainFragment=MainFragment.newInstance(floor_id, room_id);
			getSupportFragmentManager().beginTransaction().replace(R.id.activity_main_content, mainFragment, "main").commit();
		}else{
			fragment.refreshData(mFloorId, mRoomId);
		}
		setTitleActionbar(room_name+"-"+floor_name);
	}

	private void setTitleActionbar(String string){
		getSupportActionBar().setTitle(string);
	}
	@Override
	public void onTabSelected(TABS tab) {
		if(tab==TABS.DEVICE){
			mMenuItem_AddScene.setVisible(false);
			mMenuItem_ModifyScene.setVisible(false);
		}else if(tab==TABS.SCENE){
			mMenuItem_AddScene.setVisible(true);
			mMenuItem_ModifyScene.setVisible(true);
		}
		
	}
}
